package com.hq.ims.platform.busservice;

import com.google.common.collect.Lists;
import com.hq.ims.data.entity.Company;
import com.hq.ims.data.service.BaseBusInfoTrackService;
import com.hq.ims.data.service.BaseCompanyService;
import com.hq.ims.data.vo.BusInfoTrackMapstruct;
import com.hq.ims.data.vo.pic.BusiInfoTraceCompanyTreeVo;
import com.hq.mybatis.base.page.RequestPage;
import com.hq.mybatis.base.page.ReturnPage;
import com.hq.utils.util.ListUtils;
import com.hq.utils.util.ObjectUtils;
import com.hq.utils.util.StringUtils;
import com.hq.utils.vo.ReturnVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author cx
 * @date 2024/11/26 11:21:02
 * @description 项目经营一张图-平台端Service
 */
@Service
public class PlatformBusiInfoMapService {

    @Resource
    private BaseBusInfoTrackService baseBusInfoTrackService;

    @Resource
    private BaseCompanyService baseCompanyService;

    /**
     * 经营信息跟踪查询-按项目查询
     *
     * @param page  分页对象
     * @param param 查询条件对象
     * @return ReturnVo
     */
    public ReturnVo<ReturnPage<BusInfoTrackMapstruct.BusInfoTrackVo>> getByPage(RequestPage<BusInfoTrackMapstruct.BusInfoTrackVo> page,
                                                                                BusInfoTrackMapstruct.PageSearchVo param) {
        baseBusInfoTrackService.getByPage(page, param);
        return ReturnVo.buildSuccess(page.getReturnPage());
    }

    /**
     * 查询经营信息跟踪数量-按公司查询
     *
     * @param companyName 公司名称
     * @return ReturnVo
     */
    public ReturnVo<List<BusiInfoTraceCompanyTreeVo>> getBusInfoTraceCountByCompany(String companyName) {
        List<Company> companyList;
        if (StringUtils.isNotEmpty(companyName)) {
            companyList = baseCompanyService.lambdaQuery()
                    .like(Company::getCompanyName, companyName)
                    .list();
        } else {
            // 查询总公司列表
            companyList = baseCompanyService.lambdaQuery()
                    .eq(Company::getParentId, "0")
                    .list();
        }
        if (ListUtils.isNotExistsEle(companyList)) {
            return ReturnVo.buildSuccess(Lists.newArrayList());
        }
        List<BusiInfoTraceCompanyTreeVo> treeList = Lists.newArrayList();
        for (Company company : companyList) {
            BusiInfoTraceCompanyTreeVo busiInfoTraceCompanyTreeVo = baseBusInfoTrackService.getBusInfoTraceCountByCompany(Boolean.FALSE, company.getCompanyName());
            if (ObjectUtils.isNotNull(busiInfoTraceCompanyTreeVo)) {
                treeList.add(busiInfoTraceCompanyTreeVo);
            }
        }
        return ReturnVo.buildSuccess(treeList);
    }

    /**
     * 经营信息跟踪查询-按公司查询
     *
     * @param page      分页对象
     * @param companyId 公司id
     * @return ReturnVo
     */
    public ReturnVo<ReturnPage<BusInfoTrackMapstruct.BusInfoTrackVo>> getBusiInfoTrackByPage(RequestPage<BusInfoTrackMapstruct.BusInfoTrackVo> page,
                                                                                             String companyId) {
        BusInfoTrackMapstruct.PageSearchVo param = new BusInfoTrackMapstruct.PageSearchVo();
        param.setCompanyId(companyId);
        baseBusInfoTrackService.getByPage(page, param);
        return ReturnVo.buildSuccess(page.getReturnPage());
    }

    /**
     * 经营信息跟踪详情查询
     *
     * @param busInfoTrackId 跟踪id
     * @return ReturnVo
     */
    public ReturnVo<BusInfoTrackMapstruct.DetailVo> getBusiInfoTrackDetail(String busInfoTrackId) {
        BusInfoTrackMapstruct.DetailVo detailVo = baseBusInfoTrackService.getDetailVo(busInfoTrackId);
        return ReturnVo.buildSuccess(detailVo);
    }
}
